package com.cherry.mvc.controller;

import com.cherry.mvc.domain.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Controller
@RequestMapping("/user/anno/return")
public class UserAnnoReturnController {

    /**
     * 1.返回String，但是方法参数上携带Model对象
     *
     * @param model
     * @return
     */
    @RequestMapping("/testString")
    public String testString(Model model, String name) {
        User user = new User();
        user.setName(name);
        user.setAge(111);
        model.addAttribute("abc", user);
        return "user";
    }

    /**
     * 2.返回void,. 如果控制器的方法返回值编写成void，执行程序报404的异常，默认查找JSP页面没有找到。
     * 默认会跳转到testVoid.jsp的页面
     *
     * @param request
     * @param response
     * @throws Exception
     */
    @RequestMapping("/testVoid")
    public void testVoid(HttpServletRequest request, HttpServletResponse response) throws Exception {
        //转发，有另一种方式，不过需要返回特定的关键字字符串
//         request.getRequestDispatcher("/WEB-INF/pages/hello.jsp").forward(request,response);

        // 重定向，有另一种方式，不过需要返回特定的关键字字符串
//         response.sendRedirect(request.getContextPath()+"/index.jsp");

        // 设置中文乱码
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");

        // 直接会进行响应
        response.getWriter().print("你好");
    }

    /**
     * 3.ModelAndView对象是Spring提供的一个对象，可以用来调整具体的JSP视图
     *
     * @param age
     * @return
     */
    @RequestMapping("/testModelAndView")
    public ModelAndView testModelAndView(int age) {
        ModelAndView modelAndView = new ModelAndView();
        User user = new User();
        user.setAge(age);
        user.setName("哈哈哈");
        modelAndView.addObject("cba", user);
        modelAndView.setViewName("user");
        return modelAndView;
    }

    /**
     * 4.关键字转发和重定向
     *
     * @return
     */
    @RequestMapping("/testForward")
    public String testForward() {
        return "forward:/WEB-INF/pages/hello.jsp";
    }

    @RequestMapping("/testRedirect")
    public String testRedirect() {
        return "redirect:/index.jsp";
    }


    /**
     * 模拟异步请求响应，请求json，响应json
     */
    @RequestMapping(value = "/testAjax1", method = RequestMethod.POST)
    public @ResponseBody
    User testAjax1(@RequestBody User user) {
        user.setName("返回值" + user.getName());
        user.setAge(user.getAge() + 1024);
        return user;
    }

    /**
     * 哪里需要转换json就加上@ResponseBody
     * 模拟异步请求响应，请求表单，响应json
     */
    @RequestMapping(value = "/testAjax2", method = RequestMethod.POST)
    public @ResponseBody
    User testAjax2(User user) {
        user.setName("返回值99999" + user.getName());
        user.setAge(user.getAge() + 2048);
        return user;
    }

}
